package reuseplugin.views.eintragEditor;

import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.part.*;
import org.eclipse.jface.viewers.*;
import org.eclipse.swt.graphics.Image;
import org.eclipse.jface.action.*;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.ui.*;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.SWT;
import client.Client;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.custom.CCombo;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Table;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.layout.GridData;

/**
 * This sample class demonstrates how to plug-in a new workbench view. The view
 * shows data obtained from the model. The sample creates a dummy model on the
 * fly, but a real implementation would connect to the model available either in
 * this or another plug-in (e.g. the workspace). The view is connected to the
 * model using a content provider.
 * <p>
 * The view uses a label provider to define how model objects should be
 * presented in the view. Each view can present the same model objects using
 * different labels and icons, if needed. Alternatively, a single label provider
 * can be shared between views in order to ensure that objects of the same type
 * are presented in the same way everywhere.
 * <p>
 */

public class EintragEditorView extends ViewPart {
	private Action action1;
	private Action action2;
	private Action doubleClickAction;
	private Composite top = null;
	private Text textName = null;
	private Text textAreaBeschreibung = null;
	private Text textAreaTags = null;
	private Tree treeKategorie = null;
	private TreeViewer treeViewer = null;
	private Text textDatei = null;
	private Button buttonDatei = null;
	private Button buttonSpeichern = null;
	private Button buttonAbbrechen = null;
	private Table tableDateien = null;
	private TableViewer tableViewer = null;
	private CLabel cLabelName = null;
	private CLabel cLabelBeschreibung = null;
	private CLabel cLabelTags = null;
	private CLabel cLabelDateien = null;
	private CLabel cLabelKategorie = null;
	private CLabel cLabelDatei = null;

	class ViewContentProvider implements IStructuredContentProvider {
		public void inputChanged(Viewer v, Object oldInput, Object newInput) {
		}

		public void dispose() {
		}

		public Object[] getElements(Object parent) {
			return new Object[0];
		}
	}

	class ViewLabelProvider extends LabelProvider implements
			ITableLabelProvider {
		public String getColumnText(Object obj, int index) {
			return getText(obj);
		}

		public Image getColumnImage(Object obj, int index) {
			return getImage(obj);
		}

		public Image getImage(Object obj) {
			return PlatformUI.getWorkbench().getSharedImages().getImage(
					ISharedImages.IMG_OBJ_ELEMENT);
		}
	}

	class NameSorter extends ViewerSorter {
	}

	/**
	 * The constructor.
	 */
	public EintragEditorView() {
	}

	/**
	 * This is a callback that will allow us to create the viewer and initialize
	 * it.
	 */
	public void createPartControl(Composite parent) {
		GridData gridData13 = new GridData();
		gridData13.horizontalAlignment = GridData.FILL;
		gridData13.horizontalSpan = 2;
		gridData13.verticalAlignment = GridData.CENTER;
		GridData gridData12 = new GridData();
		gridData12.horizontalAlignment = GridData.FILL;
		gridData12.verticalAlignment = GridData.CENTER;
		GridData gridData11 = new GridData();
		gridData11.horizontalAlignment = GridData.FILL;
		gridData11.verticalAlignment = GridData.CENTER;
		GridData gridData10 = new GridData();
		gridData10.horizontalAlignment = GridData.FILL;
		gridData10.verticalAlignment = GridData.CENTER;
		GridData gridData9 = new GridData();
		gridData9.horizontalAlignment = GridData.FILL;
		gridData9.verticalAlignment = GridData.CENTER;
		GridData gridData8 = new GridData();
		gridData8.horizontalAlignment = GridData.FILL;
		gridData8.verticalAlignment = GridData.CENTER;
		GridData gridData7 = new GridData();
		gridData7.horizontalSpan = 2;
		gridData7.horizontalAlignment = GridData.FILL;
		gridData7.verticalAlignment = GridData.FILL;
		gridData7.verticalSpan = 3;
		GridData gridData6 = new GridData();
		gridData6.horizontalAlignment = GridData.FILL;
		gridData6.grabExcessHorizontalSpace = true;
		gridData6.grabExcessVerticalSpace = false;
		gridData6.horizontalSpan = 2;
		gridData6.verticalSpan = 2;
		gridData6.verticalAlignment = GridData.FILL;
		GridData gridData5 = new GridData();
		gridData5.horizontalAlignment = GridData.FILL;
		gridData5.verticalAlignment = GridData.CENTER;
		GridData gridData4 = new GridData();
		gridData4.horizontalAlignment = GridData.FILL;
		gridData4.verticalAlignment = GridData.CENTER;
		GridData gridData3 = new GridData();
		gridData3.horizontalAlignment = GridData.FILL;
		gridData3.verticalAlignment = GridData.CENTER;
		GridData gridData2 = new GridData();
		gridData2.horizontalAlignment = GridData.FILL;
		gridData2.verticalAlignment = GridData.CENTER;
		GridData gridData1 = new GridData();
		gridData1.verticalSpan = 10;
		gridData1.verticalAlignment = GridData.FILL;
		gridData1.horizontalAlignment = GridData.FILL;
		GridData gridData = new GridData();
		gridData.horizontalSpan = 3;
		gridData.horizontalAlignment = GridData.FILL;
		gridData.verticalAlignment = GridData.FILL;
		gridData.grabExcessVerticalSpace = false;
		gridData.verticalSpan = 6;
		gridData.grabExcessHorizontalSpace = true;
		GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 8;
		gridLayout.makeColumnsEqualWidth = true;
		top = new Composite(parent, SWT.NONE);
		top.setLayout(gridLayout);
		cLabelKategorie = new CLabel(top, SWT.NONE);
		cLabelKategorie.setText("Kategorie:");
		cLabelKategorie.setLayoutData(gridData2);
		cLabelName = new CLabel(top, SWT.NONE);
		cLabelName.setText("Name:");
		cLabelName.setLayoutData(gridData3);
		textName = new Text(top, SWT.BORDER);
		textName.setText("Name");
		textName.setLayoutData(gridData12);
		Label filler90 = new Label(top, SWT.NONE);
		Label filler91 = new Label(top, SWT.NONE);
		Label filler92 = new Label(top, SWT.NONE);
		Label filler93 = new Label(top, SWT.NONE);
		Label filler40 = new Label(top, SWT.NONE);
		treeKategorie = new Tree(top, SWT.NONE);
		treeKategorie.setLayoutData(gridData1);
		cLabelBeschreibung = new CLabel(top, SWT.NONE);
		cLabelBeschreibung.setText("Beschreibung:");
		cLabelBeschreibung.setLayoutData(gridData4);
		textAreaBeschreibung = new Text(top, SWT.MULTI | SWT.WRAP
				| SWT.V_SCROLL);
		textAreaBeschreibung.setText("Beschreibung");
		textAreaBeschreibung.setLayoutData(gridData7);
		cLabelDatei = new CLabel(top, SWT.NONE);
		cLabelDatei.setText("Neue Datei hochladen:");
		textDatei = new Text(top, SWT.BORDER);
		textDatei.setLayoutData(gridData13);
		buttonDatei = new Button(top, SWT.NONE);
		buttonDatei.setText("Browse");
		buttonDatei.setLayoutData(gridData9);
		buttonDatei
				.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
					public void widgetSelected(
							org.eclipse.swt.events.SelectionEvent e) {
						System.out.println("widgetSelected()"); // TODO
																// Auto-generated
																// Event stub
																// widgetSelected()
					}
				});
		Label filler1 = new Label(top, SWT.NONE);
		Label filler15 = new Label(top, SWT.NONE);
		Label filler16 = new Label(top, SWT.NONE);
		Label filler38 = new Label(top, SWT.NONE);
		Label filler39 = new Label(top, SWT.NONE);
		Label filler = new Label(top, SWT.NONE);
		Label filler2 = new Label(top, SWT.NONE);
		Label filler9 = new Label(top, SWT.NONE);
		Label filler18 = new Label(top, SWT.NONE);
		Label filler37 = new Label(top, SWT.NONE);
		cLabelTags = new CLabel(top, SWT.NONE);
		cLabelTags.setText("Tags:");
		cLabelTags.setLayoutData(gridData5);
		textAreaTags = new Text(top, SWT.MULTI | SWT.WRAP | SWT.V_SCROLL);
		textAreaTags.setText("Tags");
		textAreaTags.setLayoutData(gridData6);
		cLabelDateien = new CLabel(top, SWT.NONE);
		cLabelDateien.setText("Dateien:");
		cLabelDateien.setLayoutData(gridData8);
		tableDateien = new Table(top, SWT.NONE);
		tableDateien.setHeaderVisible(true);
		tableDateien.setLayoutData(gridData);
		tableDateien.setLinesVisible(true);
		Label filler4 = new Label(top, SWT.NONE);
		Label filler10 = new Label(top, SWT.NONE);
		Label filler11 = new Label(top, SWT.NONE);
		Label filler12 = new Label(top, SWT.NONE);
		Label filler13 = new Label(top, SWT.NONE);
		Label filler14 = new Label(top, SWT.NONE);
		Label filler19 = new Label(top, SWT.NONE);
		Label filler20 = new Label(top, SWT.NONE);
		Label filler21 = new Label(top, SWT.NONE);
		Label filler22 = new Label(top, SWT.NONE);
		Label filler24 = new Label(top, SWT.NONE);
		Label filler25 = new Label(top, SWT.NONE);
		Label filler26 = new Label(top, SWT.NONE);
		Label filler27 = new Label(top, SWT.NONE);
		Label filler29 = new Label(top, SWT.NONE);
		Label filler30 = new Label(top, SWT.NONE);
		Label filler31 = new Label(top, SWT.NONE);
		Label filler32 = new Label(top, SWT.NONE);
		Label filler5 = new Label(top, SWT.NONE);
		Label filler6 = new Label(top, SWT.NONE);
		Label filler7 = new Label(top, SWT.NONE);
		Label filler8 = new Label(top, SWT.NONE);
		Label filler42 = new Label(top, SWT.NONE);
		buttonSpeichern = new Button(top, SWT.NONE);
		buttonSpeichern.setText("Speichern");
		buttonSpeichern.setLayoutData(gridData10);
		buttonSpeichern
				.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
					public void widgetSelected(
							org.eclipse.swt.events.SelectionEvent e) {
						System.out.println("widgetSelected()"); // TODO
																// Auto-generated
																// Event stub
																// widgetSelected()
					}
				});
		buttonAbbrechen = new Button(top, SWT.NONE);
		buttonAbbrechen.setText("Abbrechen");
		buttonAbbrechen.setLayoutData(gridData11);
		buttonAbbrechen
				.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
					public void widgetSelected(
							org.eclipse.swt.events.SelectionEvent e) {
						System.out.println("widgetSelected()"); // TODO
																// Auto-generated
																// Event stub
																// widgetSelected()
					}
				});
		tableViewer = new TableViewer(tableDateien);
		treeViewer = new TreeViewer(treeKategorie);
		makeActions();
		hookContextMenu();
		hookDoubleClickAction();
		contributeToActionBars();
	}

	private void hookContextMenu() {
		MenuManager menuMgr = new MenuManager("#PopupMenu");
		menuMgr.setRemoveAllWhenShown(true);
		menuMgr.addMenuListener(new IMenuListener() {
			public void menuAboutToShow(IMenuManager manager) {
				EintragEditorView.this.fillContextMenu(manager);
			}
		});
		Menu menu = menuMgr.createContextMenu(tableViewer.getControl());
		tableViewer.getControl().setMenu(menu);
		getSite().registerContextMenu(menuMgr, tableViewer);
	}

	private void contributeToActionBars() {
		IActionBars bars = getViewSite().getActionBars();
		fillLocalPullDown(bars.getMenuManager());
		fillLocalToolBar(bars.getToolBarManager());
	}

	private void fillLocalPullDown(IMenuManager manager) {
		manager.add(action1);
		manager.add(new Separator());
		manager.add(action2);
	}

	private void fillContextMenu(IMenuManager manager) {
		manager.add(action1);
		manager.add(action2);
		// Other plug-ins can contribute there actions here
		manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
	}

	private void fillLocalToolBar(IToolBarManager manager) {
		manager.add(action1);
		manager.add(action2);
	}

	private void makeActions() {
		action1 = new Action() {
			public void run() {
				showMessage("Action 1 executed");
			}
		};
		action1.setText("Action 1");
		action1.setToolTipText("Action 1 tooltip");
		action1.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages()
				.getImageDescriptor(ISharedImages.IMG_OBJS_INFO_TSK));

		action2 = new Action() {
			public void run() {
				showMessage("Action 2 executed");
			}
		};
		action2.setText("Action 2");
		action2.setToolTipText("Action 2 tooltip");
		action2.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages()
				.getImageDescriptor(ISharedImages.IMG_OBJS_INFO_TSK));
		doubleClickAction = new Action() {
			public void run() {
				ISelection selection = tableViewer.getSelection();
				Object obj = ((IStructuredSelection) selection)
						.getFirstElement();
				showMessage("Double-click detected on " + obj.toString());
			}
		};
	}

	private void hookDoubleClickAction() {
		tableViewer.addDoubleClickListener(new IDoubleClickListener() {
			public void doubleClick(DoubleClickEvent event) {
				doubleClickAction.run();
			}
		});
	}

	private void showMessage(String message) {
		MessageDialog.openInformation(tableViewer.getControl().getShell(),
				"Eintrag Editor View", message);
	}

	/**
	 * Passing the focus request to the viewer's control.
	 */
	public void setFocus() {
		tableViewer.getControl().setFocus();
	}
} // @jve:decl-index=0:visual-constraint="10,10,971,260"